﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;


public partial class back_AddMenu : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Menus objMenu;
    List<Menus> objlstMenu = new List<Menus>();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            fillMainMenu();
            fillOrderMenu();
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                objMenu = new Menus(_dataObject);
                objMenu.Id = Convert.ToInt32(Request.QueryString["id"]);
                objMenu.GetMenuById();
                if (!string.IsNullOrEmpty(objMenu.Name))
                {
                    txtMenuName.Text = objMenu.Name;
                    if (!string.IsNullOrEmpty(objMenu.Link))
                        txtMenuLink.Text = objMenu.Link;
                    else
                        txtMenuName.Text = "";
                    if (objMenu.IsParent == true)
                    {
                        chkIsParent.Checked = true;
                        drpMenuOrder.Visible = true;
                        drpMenuOrder.SelectedValue = Convert.ToString(objMenu.OrderId);
                    }
                    else
                    {
                        drpMenuOrder.Visible = false;
                        chkIsParent.Checked = false;
                    }
                    drpMainMenu.SelectedValue = Convert.ToString(objMenu.Id);

                    drpMainMenu_SelectedIndexChanged(sender, e);


                    if (drpSubMenu.Items.Count > 0)
                    {
                        if (!string.IsNullOrEmpty(Convert.ToString(objMenu.ParentId)))
                            drpSubMenu.SelectedValue = Convert.ToString(objMenu.ParentId);
                        else
                            drpSubMenu.SelectedIndex = 0;
                    }
                    if (!string.IsNullOrEmpty(Convert.ToString(objMenu.OrderId)))
                        drpMenuOrder.SelectedValue = Convert.ToString(objMenu.OrderId);
                    else
                        drpMenuOrder.SelectedIndex = 0;
                }
            }
        }
    }

    private void fillOrderMenu()
    {
        drpMenuOrder.Items.Clear();
        objMenu = new Menus(_dataObject);
        int count = objMenu.GetParentMenus().Count();
        drpMenuOrder.Items.Add(new System.Web.UI.WebControls.ListItem("Select Order", "0"));
        if (count > 0)
        {
            for (int i = 1; i < count + 1; i++)
            {
                drpMenuOrder.Items.Add(new System.Web.UI.WebControls.ListItem(i.ToString(), i.ToString()));
            }
        }
        drpMenuOrder.Items.Add(new System.Web.UI.WebControls.ListItem("Add New", "add"));
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        objMenu = new Menus(_dataObject);
        objMenu.Name = txtMenuName.Text;
        objMenu.IsParent = chkIsParent.Checked;
        objMenu.Link = txtMenuLink.Text;

        if (!chkIsParent.Checked)
        {
            if (drpSubMenu.SelectedIndex > 0)
                objMenu.ParentId = Convert.ToInt32(drpSubMenu.SelectedValue);
            else if (drpMainMenu.SelectedIndex > 0)
                objMenu.ParentId = Convert.ToInt32(drpMainMenu.SelectedValue);
        }
        if (drpMenuOrder.SelectedIndex > 0)
        {
            if (drpMenuOrder.SelectedValue.ToString().Trim().ToLower() == "add")
                objMenu.OrderId = drpMenuOrder.Items.Count - 1;
            else
                objMenu.OrderId = Convert.ToInt32(drpMenuOrder.SelectedValue);
        }

        if (string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            if (checkIfMenuExists(objMenu.Name))
            {
                divMessage.InnerHtml = "<strong>On snap!</strong>&nbsp;Menu with the same name Exists!";
                divMessage.Attributes.Add("class", "alert alert-success");
            }
            else
            {
                objMenu.CreateMenu();

                divMessage.InnerHtml = "<strong>Well done!</strong>&nbsp;Menu inserted successfully!";
                divMessage.Attributes.Add("class", "alert alert-success");
            }
        }
        else
        {
            objMenu.Id = Convert.ToInt32(Request.QueryString["id"]);
            objMenu.UpdateMenu();

            divMessage.InnerHtml = "<strong>Well done!</strong>&nbsp;Menu updated successfully!";
            divMessage.Attributes.Add("class", "alert alert-success");
        }

        clearFields();
        fillMainMenu();
        fillOrderMenu();
    }
    private bool checkIfMenuExists(string menuName)
    {
        Menus checkMenu = new Menus(_dataObject);
        checkMenu.Name = menuName;
        checkMenu.GetMenuByName();

        if (checkMenu.Id != 0)
            return true;
        else
            return false;
    }
    private void fillMainMenu()
    {
        objMenu = new Menus(_dataObject);
        objlstMenu = objMenu.GetMenuNameById();

        if (objlstMenu.Count > 0)
        {
            drpMainMenu.DataSource = objlstMenu;
            drpMainMenu.DataTextField = "Name";
            drpMainMenu.DataValueField = "Id";
            drpMainMenu.DataBind();
            drpMainMenu.Items.Insert(0, "Select Menu");
        }
    }

    private void clearFields()
    {
        txtMenuName.Text = "";
        txtMenuLink.Text = "";
        chkIsParent.Checked = false;
        drpMainMenu.SelectedIndex = -1;
        drpSubMenu.SelectedIndex = -1;
    }

    protected void drpMainMenu_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpMainMenu.SelectedIndex > 0)
        {
            objMenu = new Menus(_dataObject);
            objMenu.ParentId = Convert.ToInt32(drpMainMenu.SelectedValue);
            objMenu.Name = drpMainMenu.SelectedItem.Text;
            objlstMenu = objMenu.GetMenuNameByParentId();
            if (objlstMenu.Count > 0)
            {
                drpSubMenu.DataSource = objlstMenu;
                drpSubMenu.DataTextField = "Name";
                drpSubMenu.DataValueField = "Id";
                drpSubMenu.DataBind();
                drpSubMenu.Items.Insert(0, "Select Sub Menu");
            }
            else
            {
                drpSubMenu.DataSource = null;
                drpSubMenu.DataBind();
            }
        }
    }

}
